import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
# %matplotlib inline
dataset = pd.read_csv('Malicious0520.csv')
malicious = pd.read_csv('Malicious.csv')

# Index(['Pregnancies', 'Glucose', 'BloodPressure', 'SkinThickness', 'Insulin', 'BMI', 'DiabetesPedigreeFunction', 'Age', 'malware'], dtype='object')

dataset.head()
print("dimension of dataset data: {}".format(dataset.shape))
print(dataset.describe())
print(dataset.groupby('malware').size())

validation_size = 0.20
seed =7
scoring= 'accuracy'

from sklearn.model_selection import train_test_split,cross_val_score,cross_validate
from sklearn.model_selection import KFold

#依据标签y，按原数据y中各类比例，分配给train和test，使得train和test中各类数据的比例与原数据集一样。random_state 它的用途是在随机划分训练集和测试集时候，划分的结果并不是那么随机，也即，确定下来random_state是某个值后，重复调用这个函数，划分结果是确定的。
X_train, X_test, y_train, y_test = train_test_split(dataset.loc[:, dataset.columns != 'malware'], dataset['malware'], test_size=validation_size , random_state=seed)
Xp_train, Xp_test, yp_train, yp_test = train_test_split(malicious.loc[:, malicious.columns != 'malware'], malicious['malware'], test_size=validation_size , random_state=seed)


import pandas
from pandas.tools.plotting import scatter_matrix
#import matplotlib.pyplotas plt
from sklearn import model_selection
from sklearn.metrics import classification_report
from sklearn.metrics import confusion_matrix
from sklearn.metrics import accuracy_score
from sklearn.linear_model import LogisticRegression
from sklearn.tree import DecisionTreeClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from sklearn.naive_bayes import GaussianNB
from sklearn.svm import SVC

models =[]
models.append(('LR', LogisticRegression()))
models.append(('LDA', LinearDiscriminantAnalysis()))
models.append(('KNN', KNeighborsClassifier()))
models.append(('CART', DecisionTreeClassifier())
models.append(('NB', GaussianNB()))
models.append(('SVM', SVC()))
#evaluate each model in turn
results =[]
names =[]
for name, model in models:
    kfold= model_selection.KFold(n_splits=10, random_state=seed)
    cv_results= model_selection.cross_val_score(model,X_train, y_train, cv=kfold, scoring=scoring)
    results.append(cv_results)

    names.append(name)
    msg= "%s: %f (%f)" % (name, cv_results.mean(),cv_results.std())
    print(msg)

cart =DecisionTreeClassifier()
cart.fit(X_train,y_train)
predictions3 = cart.predict(Xp_train)
print(accuracy_score(yp_train, predictions3))
print(confusion_matrix(yp_train, predictions3))
print(classification_report(yp_train, predictions3))

# from sklearn.externals import joblib# 保存模型到 model.joblib 文件
# joblib.dump(cart, "model.joblib" ,compress=1)# 加载模型文件，生成模型对象
# new_model = joblib.load("model.joblib")
# new_pred_data = [[0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,1,0,0,0,0,1,0,1,0,0,0,0,1,1,0,0,0,1,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1,1],[0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1,0],[0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1,0],[0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,1,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1,0],[0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1,0]]
# oo = new_model.predict(new_pred_data)
# print (oo)